<?php

/**
 * --------------------------------
 * Default TestController
 *
 * 课程管理的统一类  TestController
 * 
 * --------------------------------
 */

class TestController extends BaseController{

 // /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }
	/**
	 * 查询考试信息接口 
	 * @param
	 * @return Response
	 */
	public function index(){
     	
     	$column = Input::get('column');
     	$content = Input::get('content');

     	if(null==$content || null== $column){
			$column ='';
			$content='';
		}
		$result = DB::table('test')
					 ->leftjoin('classes_test','classes_test.test_id','=','test.id')
					 ->select('test.*','classes_test.classes_id','classes_test.rank',
					 	      'classes_test.remark')
					 ->orderBy('created_at','DESC');
					 
		if($column != ''){
			switch($column){
				case 'classes_id':
					$table = 'classes_test';
					$column = 'classes_id';
				default:
				    break;	
			}	
			$result = $result->where($table.'.'.$column,'=',$content);	
		}
		$result = $result->paginate(15)->toJson();			 
		$result = json_decode($result);
		return Response::json(['error'=>false,'result'=>$result],200);
    }

  //   /**
	 // * 查询考试信息接口 
	 // * @param
	 // * @return Response
	 // */
  //   public function show($id){
  //   	$result = DB::table('test')
		// 			 ->leftjoin('classes_test','classes_test.test_id','=','test.id')
		// 			 ->select('test.*','classes_test.classes_id','classes_test.rank',
		// 			 	      'classes_test.remark')
		// 			 ->where()

  //   }


    /**
	 * 新增考试接口 
	 * @param 
	 * @return Response
	 */
	public function store(){

		$insert_arr = array();
		$insert_arr['name'] = Input::get('name');
		$insert_arr['type'] = Input::get('type');
		$subject_id_arr = array();
		$subject_id_arr = Input::get('subject_id');
		$subject_id = implode(',', $subject_id_arr);
		$insert_arr['subject_id'] = $subject_id;
		$insert_arr['start_time'] = Input::get('start_time');
		$insert_arr['end_time'] = Input::get('end_time');
		$insert_arr['remark'] = Input::get('remark');
		$insert_arr['condition'] = 0;

        if(!Input::has("name") || !Input::has("type") || !Input::has('subject_id') ||
           !Input::has('start_time') || !Input::has('end_time')){
	 		return Response::json(['error'=>true,'message'=>"参数错误"], 201);
	 	}
	 	$id = DB::table('test')->insertGetId($insert_arr);
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$id],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'创建失败'],201);
	 	}
	}

	/**
	 * 修改考试接口 
	 * @param 
	 * @return Response
	 */
	public function update(){

	}
	
	/**
	 * 删除考试接口 
	 * @param 
	 * @return Response
	 */
	public function destroy($id){

	    $del_result = DB::table('subject')->where('id',$id)->update(array('is_del'=>1));
	    if($del_result){
	    	return Response::json(['error'=>false,'message'=>'删除成功'],200);
	    }else{
	    	return Response::json(['error'=>true,'message'=>'删除失败'],201);
	    }

	}
}	